//
//  MSLFontListView.m
//  ExtModule
//
//  Created by CPX on 2018/7/10.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MSLFontListView.h"

/**
 贺卡字体选择
 */
@interface MSLFontListView()
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>
{
    NSInteger current_font_index;
}
@property (nonatomic,strong)UICollectionView *  collectionView;

@property (nonatomic,strong)UISlider  * slider;
@property (nonatomic,strong)NSMutableArray * dataArray;//字体数据

@end


@implementation MSLFontListView
#pragma mark ------- getter
-(NSMutableArray * )dataArray{
    if (!_dataArray){
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}
-(UISlider *)slider{
    if (!_slider){
        _slider = [[UISlider alloc]init];
        _slider.minimumValue = 10;
        _slider.maximumValue = 50;
        _slider.value = 15;
        _slider.minimumTrackTintColor = [UIColor blackColor];
        _slider.maximumTrackTintColor = [UIColor lightGrayColor];
        [_slider addTarget:self action:@selector(fontSizeChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _slider;
}
-(UICollectionView* )collectionView{
    if (!_collectionView){
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 20, 20) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.allowsSelection = YES;
        [_collectionView registerClass:GreetFontCell.class forCellWithReuseIdentifier:NSStringFromClass(GreetFontCell.class)];
        
    }
    return _collectionView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self  = [super initWithFrame:frame]){
        [self createViews];
        [self getData];
        
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    CGSize  size = self.collectionView.size;
    CGFloat width  = (size.width - 20) / 3;
    CGFloat height =  (size.height - 20) / 3;
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.itemSize = CGSizeMake(width, height);
    self.collectionView.collectionViewLayout = layout;
}

-(void)createViews{
    UILabel * sliderMinLabel = [UILabel new];
    sliderMinLabel.font = [UIFont systemFontOfSize:15];
    sliderMinLabel.textColor = [UIColor blackColor];
    sliderMinLabel.text = TLString(@"greeting_cards_size");
    
    UILabel * sliderMaxLabel = [UILabel new];
    sliderMaxLabel.tag = 110;
    sliderMaxLabel.font = [UIFont systemFontOfSize:15];
    sliderMaxLabel.textColor = [UIColor blackColor];
    NSString * string  = [@"15" stringByAppendingString:TLString(@"greeting_cards_pt")];
    if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]){
        string = string.arabicString;
    }
    
    sliderMaxLabel.text = string;
    [self addSubview:sliderMinLabel];
    [self addSubview:sliderMaxLabel];
    [sliderMinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(10);
        make.leading.equalTo(self).offset(20);
        
    }];
    [sliderMaxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sliderMinLabel);
        make.trailing.equalTo(self.mas_trailing).offset(-20);
        
    }];
    
    [self addSubview:self.slider];
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sliderMinLabel);
        make.trailing.equalTo(sliderMaxLabel.mas_leading).offset(-10);
        make.leading.equalTo(sliderMinLabel.mas_trailing).offset(10);
    }];
    
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self);
        make.top.equalTo(self.slider.mas_bottom).offset(20);
    }];
  
    
    
}

#pragma mark------获取图片背景数据
-(void)getData{
    UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    
    NSLog(@"当前字体。。。 %@",font);

    
    NSArray* familyNameArray = @[@"Arial",
                                 @"Al Qalam Quran Majeed",
                                 @"Apple Chancery",
                                 @"KFGQPC Uthmanic Script HAFS",
                                 @"Kokonor",
                                 @"Noto Naskh Arabic",
                                 @"Al Qalam Quran Majeed 2",
                                 @"Scheherazade",
                                 @"SF-Mada"];


    
    NSLog(@"所有字体 %@",familyNameArray);
    [self.dataArray addObjectsFromArray:familyNameArray];
    [self.collectionView reloadData];

    

    
    
    
}

#pragma  mark ----UICollectionViewDelegate,UICollectionViewDataSource,
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GreetFontCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(GreetFontCell.class) forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];;
    cell.backgroundColor = [UIColor clearColor];
    cell.fontNameLabel.numberOfLines = 0;
    cell.fontNameLabel.adjustsFontSizeToFitWidth = YES;
    NSString * fontName = self.dataArray[indexPath.row];
    cell.fontNameLabel.text = fontName;
    UIFont  * font = [UIFont fontWithName:fontName size:14];
    
    cell.fontNameLabel.font = font;
    return cell;
    
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    current_font_index = indexPath.row;
    if (self.selectFontEvent)
    {
        NSString * fontName = self.dataArray[indexPath.row];
        UIFont  * font = [UIFont fontWithName:fontName size:self.slider.value];
        self.selectFontEvent(font,current_font_index);

    }
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(void)fontSizeChange:(UISlider* )sender{
    UILabel* label  = [self viewWithTag:110];
    NSString *string  = [NSString stringWithFormat:@"%d %@",(int)sender.value,TLString(@"greeting_cards_pt")];
    if ([[LanguageManager defaultManager].appLanguage  isEqualToString:@"ar"])
    {
        string = string.arabicString;
    }
    
    label.text = string;
    if (self.selectFontEvent)
    {

        UIFont  * font = [UIFont fontWithName:self.font_current.fontName size:sender.value];
        self.selectFontEvent(font,current_font_index);
    }
    
}

@end


@implementation GreetFontCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        [self createViews];
    }
    return self;
}
-(void)createViews
{
    self.fontNameLabel = [UILabel new];
    [self.contentView addSubview:self.fontNameLabel];
    [self.fontNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
        
    }];
    self.fontNameLabel.numberOfLines = 0;
    self.fontNameLabel.textAlignment = NSTextAlignmentCenter;
    self.fontNameLabel.font = [UIFont systemFontOfSize:13];
    self.contentView.layer.cornerRadius = 2;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.borderWidth = 0.5;
    self.contentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
}
- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    if (selected){
        self.contentView.backgroundColor = [UIColor blackColor];
        self.fontNameLabel.textColor = [UIColor whiteColor];
    }
    else{
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.fontNameLabel.textColor = [UIColor blackColor];

    }
}


@end


